<script setup lang="tsx">
import type { DataTableColumns } from 'naive-ui';
import { NCheckbox, NInput, NInputNumber } from 'naive-ui';
import { h, ref, watch } from 'vue';
import PorDictSelect from '@/components/_builtin/por-dict-select/index.vue';
import PorSelect from '@/components/_builtin/por-select/index.vue';

defineOptions({
  name: 'CodeColumnConfigForm'
});

interface Props {
  /** the edit row data */
  columns: Api.Model[];
}

const props = defineProps<Props>();

const data = ref<Api.Model[]>([]);

watch(
  () => props.columns,
  columns => {
    if (!columns) return;
    data.value = columns;
  },
  {
    deep: true,
    immediate: true
  }
);

const tableColumns: DataTableColumns<Api.Row> = [
  {
    title: '编号',
    key: 'id',
    width: '60',
    fixed: 'left'
  },
  {
    title: '名称',
    key: 'name',
    width: '140',
    fixed: 'left'
  },
  {
    title: '字段描述',
    key: 'comment',
    width: '160',
    fixed: 'left',
    render(row, index) {
      return h(NInput, {
        value: row.comment,
        onUpdateValue(v) {
          data.value[index].comment = v;
        }
      });
    }
  },
  {
    title: '字段类型',
    key: 'type',
    width: '100',
    fixed: 'left'
  },
  {
    title: 'Java 属性名',
    key: 'property',
    width: '120',
    fixed: 'left'
  },
  {
    title: 'Java 类型',
    key: 'propertyType',
    width: '200',
    fixed: 'left'
  },
  {
    title: '是否主键',
    key: 'primaryKey',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.primaryKey,
        onUpdateChecked(v) {
          data.value[index].primaryKey = v;
        }
      });
    }
  },
  {
    title: '是否自增',
    key: 'autoIncrement',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.autoIncrement,
        onUpdateChecked(v) {
          data.value[index].autoIncrement = v;
        }
      });
    }
  },
  {
    title: '是否可空',
    key: 'nullable',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.nullable,
        onUpdateChecked(v) {
          data.value[index].nullable = v;
        }
      });
    }
  },
  {
    title: '生成主键',
    key: 'generatedKey',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.generatedKey,
        onUpdateChecked(v) {
          data.value[index].generatedKey = v;
        }
      });
    }
  },
  {
    title: '大字段',
    key: 'large',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.large,
        onUpdateChecked(v) {
          data.value[index].large = v;
        }
      });
    }
  },
  {
    title: '转换器',
    key: 'conversion',
    width: '100',
    render(row, index) {
      return h(NInput, {
        value: row.conversion,
        onUpdateValue(v) {
          data.value[index].conversion = v;
        }
      });
    }
  },
  {
    title: '转换类型',
    key: 'complexPropType',
    width: '100',
    render(row, index) {
      return h(NInput, {
        value: row.complexPropType,
        onUpdateValue(v) {
          data.value[index].complexPropType = v;
        }
      });
    }
  },
  {
    title: '逻辑删除',
    key: 'logicDelete',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.logicDelete,
        onUpdateChecked(v) {
          data.value[index].logicDelete = v;
        }
      });
    }
  },
  {
    title: '乐观锁',
    key: 'version',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.version,
        onUpdateChecked(v) {
          data.value[index].version = v;
        }
      });
    }
  },
  {
    title: '乐观锁策略',
    key: 'versionStrategy',
    width: '100',
    render(row, index) {
      return h(NInput, {
        value: row.versionStrategy,
        onUpdateValue(v) {
          data.value[index].versionStrategy = v;
        }
      });
    }
  },
  {
    title: '是否加密',
    key: 'encryption',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        value: row.encryption,
        onUpdateChecked(v) {
          data.value[index].encryption = v;
        }
      });
    }
  },
  {
    title: '加密策略',
    key: 'encryption',
    width: '100',
    render(row, index) {
      return h(NInput, {
        checked: row.encryption,
        onUpdateValue(v) {
          data.value[index].encryption = v;
        }
      });
    }
  },
  {
    title: '加密模糊查询',
    key: 'encryptionQueryLike',
    width: '140',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.encryptionQueryLike,
        onUpdateChecked(v) {
          data.value[index].encryptionQueryLike = v;
        }
      });
    }
  },
  {
    title: '是否租户',
    key: 'tenantId',
    width: '100',
    render(row, index) {
      return h(NCheckbox, {
        checked: row.tenantId,
        onUpdateChecked(v) {
          data.value[index].tenantId = v;
        }
      });
    }
  },
  {
    title: '条件配置',
    key: 'searchGroup',
    width: '1500',
    children: [
      {
        title: '是否条件',
        key: 'search',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.search,
            onUpdateChecked(v) {
              data.value[index].search = v;
            }
          });
        }
      },
      {
        title: '查询关键字',
        key: 'searchKeyword',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.searchKeyword,
            onUpdateChecked(v) {
              data.value[index].searchKeyword = v;
            }
          });
        }
      },
      {
        title: '查询操作符',
        key: 'searchOperator',
        width: '120',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.searchOperator,
            dictCode: 'code_search_operator',
            clearable: true,
            onUpdateValue(v: string) {
              data.value[index]!.searchOperator = v;
            }
          });
        }
      },
      {
        title: '查询默认值',
        key: 'searchDefValue',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchDefValue,
            onUpdateValue(v) {
              data.value[index].searchDefValue = v;
            }
          });
        }
      },
      {
        title: '条件排序号',
        key: 'searchSortRank',
        width: '100',
        render(row, index) {
          return h(NInputNumber, {
            value: row.searchSortRank,
            min: 1,
            onUpdateValue(v) {
              data.value[index].searchSortRank = v;
            }
          });
        }
      },
      {
        title: '查询标题',
        key: 'searchLabel',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchLabel,
            onUpdateValue(v) {
              data.value[index].searchLabel = v;
            }
          });
        }
      },
      {
        title: '控件类型',
        key: 'searchElement',
        width: '160',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.searchElement,
            dictCode: 'code_form_element',
            clearable: true,
            onUpdateValue(v: string) {
              data.value[index]!.searchElement = v;
            }
          });
        }
      },
      {
        title: '输入提示',
        key: 'searchPlaceholder',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchPlaceholder,
            onUpdateValue(v) {
              data.value[index].searchPlaceholder = v;
            }
          });
        }
      },
      {
        title: '检验规则',
        key: 'searchRule',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchRule,
            onUpdateValue(v) {
              data.value[index].searchRule = v;
            }
          });
        }
      },
      {
        title: '校验规则提示',
        key: 'searchRuleTips',
        width: '140',
        render(row, index) {
          return h(NInput, {
            value: row.searchRuleTips,
            onUpdateValue(v) {
              data.value[index].searchRuleTips = v;
            }
          });
        }
      },
      {
        title: '数据来源类型',
        key: 'searchSourceType',
        width: '140',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.searchSourceType,
            dictCode: 'code_source_type',
            clearable: true,
            onUpdateValue(v: string) {
              data.value[index]!.searchSourceType = v;
            }
          });
        }
      },
      {
        title: '数据来源值',
        key: 'searchSourceValue',
        width: '180',
        render(row, index) {
          switch (row.searchSourceType) {
            case 'dict':
              return h(PorSelect, {
                value: row.searchSourceValue,
                url: '/setting/dict/simpleList',
                clearable: true,
                valueField: 'code',
                labelField: 'name',
                onUpdateValue(v: string) {
                  data.value[index].searchSourceValue = v;
                }
              });
            case 'enum':
              return h(PorSelect, {
                value: row.searchSourceValue,
                url: '/',
                clearable: true,
                onUpdateValue(v: string) {
                  data.value[index].searchSourceValue = v;
                }
              });
              break;
            default:
              return h(NInput, {
                value: row.searchSourceValue,
                clearable: true,
                onUpdateValue(v: string) {
                  data.value[index].searchSourceValue = v;
                }
              });
          }
        }
      },
      {
        title: '数据文本属性',
        key: 'searchDataTextAttr',
        width: '140',
        render(row, index) {
          return h(NInput, {
            value: row.searchDataTextAttr,
            onUpdateValue(v) {
              data.value[index].searchDataTextAttr = v;
            }
          });
        }
      },
      {
        title: '数据值属性',
        key: 'searchDataValueAttr',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchDataValueAttr,
            onUpdateValue(v) {
              data.value[index].searchDataValueAttr = v;
            }
          });
        }
      },
      {
        title: '数据列属性',
        key: 'searchDataColAttr',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.searchDataColAttr,
            onUpdateValue(v) {
              data.value[index].searchDataColAttr = v;
            }
          });
        }
      }
    ]
  },
  {
    title: '结果配置',
    key: 'resultGroup',
    width: '1000',
    children: [
      {
        title: '是否结果',
        key: 'result',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.result,
            onUpdateChecked(v) {
              data.value[index].result = v;
            }
          });
        }
      },
      {
        title: '列宽',
        key: 'resultWidth',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultWidth,
            onUpdateValue(v) {
              data.value[index].resultWidth = v;
            }
          });
        }
      },
      {
        title: '最小列宽',
        key: 'resultMinWidth',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultMinWidth,
            onUpdateValue(v) {
              data.value[index].resultMinWidth = v;
            }
          });
        }
      },
      {
        title: '最大列宽',
        key: 'resultMaxWidth',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultMaxWidth,
            onUpdateValue(v) {
              data.value[index].resultMaxWidth = v;
            }
          });
        }
      },
      {
        title: '列排序号',
        key: 'resultSortRank',
        width: '100',
        render(row, index) {
          return h(NInputNumber, {
            value: row.resultSortRank,
            onUpdateValue(v) {
              data.value[index].resultSortRank = v;
            }
          });
        }
      },
      {
        title: '列标题',
        key: 'resultLabel',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultLabel,
            onUpdateValue(v) {
              data.value[index].resultLabel = v;
            }
          });
        }
      },
      {
        title: '列翻译',
        key: 'resultTranslate',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.resultTranslate,
            onUpdateChecked(v) {
              data.value[index].resultTranslate = v;
            }
          });
        }
      },
      {
        title: '列翻译方法',
        key: 'resultTranslateMethod',
        width: '100',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.resultTranslateMethod,
            dictCode: 'code_translate_method',
            onUpdateValue(v: string) {
              data.value[index].resultTranslateMethod = v;
            }
          });
        }
      },
      {
        title: '列翻译值',
        key: 'resultTranslateValue',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultTranslateValue,
            onUpdateValue(v) {
              data.value[index].resultTranslateValue = v;
            }
          });
        }
      },
      {
        title: '列翻译新列',
        key: 'resultTranslateNewLabel',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.resultTranslateNewLabel,
            onUpdateValue(v) {
              data.value[index].resultTranslateNewLabel = v;
            }
          });
        }
      }
    ]
  },
  {
    title: '表单配置',
    key: 'searchGroup',
    width: '1600',
    children: [
      {
        title: '是否模型',
        key: 'model',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.model,
            onUpdateChecked(v) {
              data.value[index].model = v;
            }
          });
        }
      },
      {
        title: '模型默认值',
        key: 'modelDefValue',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.modelDefValue,
            onUpdateValue(v) {
              data.value[index].modelDefValue = v;
            }
          });
        }
      },
      {
        title: '是否表单',
        key: 'form',
        width: '100',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.form,
            onUpdateChecked(v) {
              data.value[index].form = v;
            }
          });
        }
      },
      {
        title: '是否创建字段',
        key: 'formCreate',
        width: '140',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.formCreate,
            onUpdateChecked(v) {
              data.value[index].formCreate = v;
            }
          });
        }
      },
      {
        title: '是否编辑字段',
        key: 'formUpdate',
        width: '140',
        render(row, index) {
          return h(NCheckbox, {
            checked: row.formUpdate,
            onUpdateChecked(v) {
              data.value[index].formUpdate = v;
            }
          });
        }
      },
      {
        title: '查询排序号',
        key: 'formSortRank',
        width: '100',
        render(row, index) {
          return h(NInputNumber, {
            value: row.formSortRank,
            min: 1,
            onUpdateValue(v) {
              data.value[index].formSortRank = v;
            }
          });
        }
      },
      {
        title: '字段标题',
        key: 'formLabel',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.formLabel,
            onUpdateValue(v) {
              data.value[index].formLabel = v;
            }
          });
        }
      },
      {
        title: '控件类型',
        key: 'formElement',
        width: '160',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.formElement,
            dictCode: 'code_form_element',
            onUpdateValue(v: string) {
              data.value[index].formElement = v;
            }
          });
        }
      },
      {
        title: '输入提示',
        key: 'formPlaceholder',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.formPlaceholder,
            onUpdateValue(v) {
              data.value[index].formPlaceholder = v;
            }
          });
        }
      },
      {
        title: '检验规则',
        key: 'formRule',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.formRule,
            onUpdateValue(v) {
              data.value[index].formRule = v;
            }
          });
        }
      },
      {
        title: '校验规则提示',
        key: 'formRuleTips',
        width: '140',
        render(row, index) {
          return h(NInput, {
            value: row.formRuleTips,
            onUpdateValue(v) {
              data.value[index].formRuleTips = v;
            }
          });
        }
      },
      {
        title: '数据来源类型',
        key: 'formSourceType',
        width: '140',
        render(row, index) {
          return h(PorDictSelect, {
            value: row.formSourceType,
            dictCode: 'code_source_type',
            clearable: true,
            onUpdateValue(v: string) {
              data.value[index].formSourceType = v;
            }
          });
        }
      },
      {
        title: '数据来源值',
        key: 'formSourceValue',
        width: '180',
        render(row, index) {
          switch (row.formSourceType) {
            case 'dict':
              return h(PorSelect, {
                value: row.formSourceValue,
                url: '/setting/dict/simpleList',
                clearable: true,
                valueField: 'code',
                labelField: 'name',
                onUpdateValue(v: string) {
                  data.value[index].formSourceValue = v;
                }
              });
            case 'enum':
              return h(PorSelect, {
                value: row.formSourceValue,
                url: '/',
                clearable: true,
                onUpdateValue(v: string) {
                  data.value[index].formSourceValue = v;
                }
              });
              break;
            default:
              return h(NInput, {
                value: row.formSourceValue,
                clearable: true,
                onUpdateValue(v: string) {
                  data.value[index].formSourceValue = v;
                }
              });
          }
        }
      },
      {
        title: '数据文本属性',
        key: 'formDataTextAttr',
        width: '140',
        render(row, index) {
          return h(NInput, {
            value: row.formDataTextAttr,
            onUpdateValue(v) {
              data.value[index].formDataTextAttr = v;
            }
          });
        }
      },
      {
        title: '数据值属性',
        key: 'formDataValueAttr',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.formDataValueAttr,
            onUpdateValue(v) {
              data.value[index].formDataValueAttr = v;
            }
          });
        }
      },
      {
        title: '数据列属性',
        key: 'formDataColAttr',
        width: '100',
        render(row, index) {
          return h(NInput, {
            value: row.formDataColAttr,
            onUpdateValue(v) {
              data.value[index].formDataColAttr = v;
            }
          });
        }
      }
    ]
  }
];
</script>

<template>
  <NDataTable
    :columns="tableColumns"
    :data="data"
    :pagination="false"
    :bordered="false"
    :single-line="false"
    :max-height="500"
    :scroll-x="6940"
  />
</template>

<style scoped></style>
